<?php
require_once 'class/action/Action.php';
/**
 * Core class, run commands.
 * @static
 * @package meta
 * @author alcaitiff
 */

class Core {
    /**
     * Retrieve and execute the pipeline commands
     * @param int $ticks Number of ticks to run
     * @param int $lastTick Time of last tick executed
     * @param int $tickSize Size of tick
     * @return void
     * @static
     */
    static public function run($ticks, $lastTick, $tickSize) {
        $end = $lastTick + ($ticks * $tickSize);

        $last = Pipeline::getOldestCommand();

        MessageSystem::addDebugMessage('Last tick: ' . $lastTick);

        while ($last !== null && $last <= $end) {
            $commands = Pipeline::getCommands($last);
            MessageSystem::addDebugMessage('Running the core for tick: ' . $last);
            foreach ($commands as $command) {
                $char=$command->character;
                $char->refresh($last);
                MessageSystem::setUser($char->charId);
                MessageSystem::addDebugMessage('Running the command: ' . $command->command . ' ts:' . $command->timestamp);
                if ($command->command == 'login') {
                    $char->hasChanged();
                } elseif (self::isNotDead($command->character)) {
                    //TODO: the char can do this command?
                    if($char->canDo($command->command)){
                        switch ($command->command) {
                            case 'look':
                                require_once 'class/action/Look.php';
                                $m=Look::run($command, $last);
                                MessageSystem::addCharacterMessage($m, $char->charId);
                                break;
                            case 'south':
                            case 'north':
                            case 'east':
                            case 'west':
                            case 'up':
                            case 'down':
                                require_once 'class/action/Move.php';
                                MessageSystem::addCharacterMessage(Move::run($command, $last), $char->charId);
                                break;
                            case 'open':
                            case 'close':
                               require_once 'class/action/Open.php';
                               MessageSystem::addCharacterMessage(Open::run($command, $last), $char->charId);
                               break;
                            case 'lock':
                            case 'unlock':
                               require_once 'class/action/Lock.php';
                               MessageSystem::addCharacterMessage(Lock::run($command, $last), $char->charId);
                               break;
                            case 'take':
                               require_once 'class/action/Take.php';
                               MessageSystem::addCharacterMessage(Take::run($command, $last), $char->charId);
                               break;
                            case 'drop':
                               require_once 'class/action/Drop.php';
                               MessageSystem::addCharacterMessage(Drop::run($command, $last), $char->charId);
                               break;
                            case 'put':
                               require_once 'class/action/Put.php';
                               MessageSystem::addCharacterMessage(Put::run($command, $last), $char->charId);
                               break;
                            case 'remove':
                            case 'equip':
                               require_once 'class/action/Equip.php';
                               MessageSystem::addCharacterMessage(Equip::run($command, $last), $char->charId);
                               break;
                            case 'whisper':
                               require_once 'class/action/Whisper.php';
                               MessageSystem::addCharacterMessage(Whisper::run($command, $last), $char->charId);
                               break;
                            case 'yell':
                            case 'say':
                               require_once 'class/action/Say.php';
                               MessageSystem::addCharacterMessage(Say::run($command, $last), $char->charId);
                               break;
                            case 'sit':
                               require_once 'class/action/Sit.php';
                               MessageSystem::addCharacterMessage(Sit::run($command, $last), $char->charId);
                               break;
                            case 'sleep':
                               require_once 'class/action/Sleep.php';
                               MessageSystem::addCharacterMessage(Sleep::run($command, $last), $char->charId);
                               break;
                            case 'standup':
                               require_once 'class/action/Standup.php';
                               MessageSystem::addCharacterMessage(Standup::run($command, $last), $char->charId);
                               break;
                        }
                    }else{
                        MessageSystem::addCharacterMessage("You can't do this now", $char->charId);
                        MessageSystem::addCharacterMessage($char, $char->charId);
                    }
                }
            }
            $last = Pipeline::getOldestCommand();
        }
        //MessageSystem::addDebugMessage('Stoping the core');
    }
    /**
     * Verify if char is dead and send a message if is
     * @param Char $char
     * @return boolean
     * @static
     */
    static public function isNotDead($char) {
        if ($char->isDead()) {
            MessageSystem::addCharacterMessage('You are dead!', $char->charId);
            return false;
        } else {
            return true;
        }
    }

}